package service;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.DateKits;
import common.kit.PageSql;
import model.EquipmentTypes;
import model.Quipment;
import model.Staff;
import model.Station;

public class QuipmentService extends BaseService {

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tq.* , ts.name AS servicerName " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_quipment tq ") ;
	    pageSql.join("t_servicer ts", "ts.id = tq.servicerId");
		pageSql.setEqualCondition("tq.stationId") ; //点位	 
		pageSql.setEqualCondition("tq.factor","全部") ; 
	    pageSql.orderBy(" tq.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		List<Record> list = page.getList();
		for (Record record : list) {
			record.set("acceptanceTime", DateKits.toStr(record.getDate("acceptanceTime")));		
			record.set("networkingTime", DateKits.toStr(record.getDate("networkingTime")));		
		}
		return page ;
	}

	public Ret save(Quipment quipment , Staff staff , Station station) {
		EquipmentTypes.dao.save(station.getId(), quipment.getFactor());
		quipment.setStationId(station.getId());
		quipment.setRecorder(staff.getName());
		quipment.setServicerId(staff.getServicerId());
		quipment.save();
	    return ok() ;
	}
	
	public Ret delete(Long id) {
		Quipment quipment = Quipment.dao.findById(id);
		if (null == quipment) {
			return fail();
		}
		quipment.delete() ;
		return ok() ;
	}

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		kv.set("ts.type" , kv.get("type"));
		String select = " tq.id,tq.factor,tq.name,tq.model,ts.name AS stationName,tsr.name AS servicerName1,tc.name AS companyName2 " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_quipment tq ") ;
	    pageSql.join(" t_station ts " , " ts.id = tq.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tq.servicerId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setEqualCondition("tq.stationId") ; //点位	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setEqualCondition("ts.type") ;
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" tq.createTime ASC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Record detail(Long id) {
		Kv kv = Kv.by("tq.id", id);
		String select = " ts.type,tq.factor,tq.name,tq.model,tq.manufacture,tq.factoryNum,tq.certificationNo,tq.testMethod,tq.detectionLimit,tq.analysisCycle,tq.range,tq.reagent1,tq.reagent2,tq.reagent3,tq.standardCurve,tq.acceptanceTime,tq.acceptanceCompany,tq.networkingTime,tq.recorder,tq.flowRange,ts.name AS stationName,tsr.name AS servicerName1,tc.name AS companyName2 " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_quipment tq ") ;
	    pageSql.join(" t_station ts " , " ts.id = tq.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tq.servicerId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
	    pageSql.setEqualCondition("tq.id");
	    pageSql.orderBy(" tq.createTime ASC ");
		return pageSql.findFirst() ;
    }
}
